/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import helper.Utilities;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 *
 * @author Hallucinogen
 */
public class Handshake {
    public Handshake(){
        mPstrlen    = 19;
        mPstr       = Utilities.DEFAULT_PROTOCOL;
        mReserved   = new byte[8];
        mFileNameHashed = "";
    }

    public Handshake(byte pstrlen, String pstr, byte[] reserved, String fileName){
        mPstrlen        = pstrlen;
        mPstr           = pstr;
        mReserved       = reserved;
        try {
            mFileNameHashed = Utilities.convertToSHA1(fileName);
        } catch (Exception ex) {

        }
    }

    public byte getPstrlen() {
        return mPstrlen;
    }

    public String getPstr() {
        return mPstr;
    }

    public byte[] getReserved() {
        return mReserved;
    }

    public String getFileName(){
        return mFileNameHashed;
    }

    public void setFileName(String nama){
        try {
            mFileNameHashed = Utilities.convertToSHA1(nama);
        } catch (Exception ex) {

        }
    }

    @Override
    public String toString() {
        String res = (char)mPstrlen + mPstr;

        for (int i = 0; i < 8; ++i) {
            res += (char)mReserved[i];
        }
        
        res = res + mFileNameHashed;
        return res;
    }

    public byte[] toByte() {
        String str = toString();
        byte[] retval = new byte[str.length()];

        for (int i = 0; i < str.length(); ++i)
            retval[i] = (byte)str.charAt(i);

        return retval;
    }

    public static Handshake fromString(String str) {
        Handshake retval = new Handshake();
        int ptr = 0;
        String temp;

        /** baca pstrlen */
        retval.mPstrlen = (byte)str.charAt(0);

        /** baca protocol */
        retval.mPstr = "";
        for (int i = 0; i < retval.mPstrlen; ++i){
            retval.mPstr += (char)str.charAt(1 + i);
        }

        /** baca reserved */
        ptr = retval.mPstrlen + 1;
        for (int i = 0; i < 8; ++i){
            retval.mReserved[i] = (byte)str.charAt(ptr + i);
        }

        /** baca filename */
        ptr = ptr + 8;

        String fileName = str.substring(ptr);

        for (int i = 0; i < fileName.length(); ++i)
            retval.mFileNameHashed += (char)fileName.charAt(i);

        return retval;
    }

    public static Handshake fromByte(byte[] bin) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < bin.length; ++i)
            sb.append((char)bin[i]);

        return fromString(sb.toString());
    }

    public static void main(String[] args) {
        Handshake hand = new Handshake();

        for (int i = 0 ; i < 8; ++i)
            hand.mReserved[i] = (byte)i;

        Handshake hand2 = Handshake.fromString(hand.toString());

        System.out.println(hand2.toString());

        for (int i = 0 ; i < 8; ++i){
            System.out.println(hand2.mReserved[i]);
        }
    }


    private byte mPstrlen;
    private String mPstr;
    private byte[] mReserved;
    private String mFileNameHashed;
}
